package com.itcast.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itcast.common.res.ResultData;
import com.itcast.util.OutUtil;
import org.springframework.ui.Model;
import com.itcast.pojo.User;
import com.itcast.service.Userservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private Userservice userservice;


    @RequestMapping("/upPwd")
    public String upPwd(){

        return null;
    }

    @RequestMapping("/login")
    @ResponseBody
    public String login(User userInfo, Model model, HttpSession session) {
        User userId=userservice.getById(userInfo.getUserName());
        if(userId==null){
            //账号不存在
            model.addAttribute("msg","账号不存在");
            return "login.html";

        }
        if(userId.getPassword().equals(userInfo.getPassword())){
            //登录成功
            //信息保存到session
            session.setAttribute("loginUser",userId);
            return "admin/login-2";
        }
        //密码不正确
        model.addAttribute("msg","密码不正确");
        return "login.html";
    }

    public String index(){
        return "admin/login-2";
    }

    @RequestMapping("/register")
    @ResponseBody
    public String register(User UserInfo, String password){
        userservice.save(UserInfo);
        return  "admin/login-2";
    }

    @RequestMapping(value = "/login3",method = RequestMethod.POST)
    @ResponseBody
    public String login3(HttpServletResponse response, HttpServletRequest request, HttpSession session, @RequestBody Map<String, String> conditionData){
        User user =(User) session.getAttribute("users");
        Map<String, Object> jsonMap = new HashMap<String, Object>();
       if (user==null){
           QueryWrapper<User> userWrapper =new QueryWrapper<User>();
           userWrapper.eq("phone",conditionData.get("phone").trim());
           userWrapper.eq("password",conditionData.get("password").trim());
           user =userservice.getOne(userWrapper);
           session.setAttribute("users",user);
           session.setAttribute("userInfo",user);
           System.out.println(user.getUid());
           if (user!=null){
               jsonMap.put("users",user);
               jsonMap.put("falge","ok");
           }else {
               jsonMap.put("falge","no");
           }

       }else {
           jsonMap.put("users",user);
           jsonMap.put("falge","ok");
       }
        String json = JSON.toJSONString(jsonMap);//加入fastjson-1.2.13.jar包
        OutUtil.outcase(json,response);
        return null;

    }
    /**
     * 用户登录
     * @param phone
     * @param password
     * @param session
     * @return
     */
    @RequestMapping("/userLogin")
    public ModelAndView userLogin(String phone, String password, HttpSession session, ModelAndView mv) {
        User user = userservice.userLogin(phone,password);
        if (user != null){
            mv.setViewName("/qd/index.html");
            session.setAttribute("userInfo",user);
            session.setAttribute("users",user);
        }else {
            mv.addObject("msg","手机号或密码错误!");
            mv.setViewName("redirect:/qd/login.html");
        }
        return mv;
    }

    /**
     * 修改头像
     */
    @ResponseBody
    @PostMapping("/updateAvatar")
    public ResultData updateAvatar(@RequestBody Map<String,String> map){
        ResultData rc=new ResultData();
        try {
            Integer userId = Integer.valueOf(map.get("userId"));
            String imgUrl = map.get("imgUrl");
            rc.setData(userservice.updateAvatar(userId,imgUrl));
        }catch (Exception e){
            e.printStackTrace();
        }
        return rc;
    }

    /**
     * 注册
     */
    @ResponseBody
    @RequestMapping("/userRegister")
    public  Map userRegister(String phone,String password){
        Map<String,Object> map = new HashMap<String,Object>();
        int result = userservice.register(phone,password);
        if (result>0){
            map.put("status",1);
        }else {
            map.put("status",0);
        }
        return map;
    }

    /**
     * 个人中心信息展示
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping("/showUserInfo")
    public  User userRegister(String userId){
        User user = userservice.getById(userId);
        return user;
    }

    /**
     * 个人信息修改
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateUserInfo")
    public  Map updateUserInfo(User user){
        int result = userservice.updateUserInfo(user);
        Map<String,Object> map = new HashMap<String,Object>();
        if (result>0){
            map.put("status","success");
        }else {
            map.put("status","error");
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/updateUserPassword")
    public  Map updateUserPassword(String uid,String password){
        int result = userservice.updateUserPassword(uid,password);
        String formerP = userservice.getById(uid).getPassword();
        Map<String,Object> map = new HashMap<String,Object>();

        if (formerP.equals(password)){
            if (result>0){
                map.put("status","success");
            }else {
                map.put("status","error");
            }
        }else {
            map.put("status","userError");
        }
        return map;
    }


}

